package telasDeAlteracoes;

import entidade.Funcionario;
import java.sql.Date;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author
 * Thomas Magnno Martins
 * Alexandre Junior Martins
 * Wanderson Ferreira
 */
public class AlteracaoFuncionario extends javax.swing.JFrame {

    Funcionario fun;

    public AlteracaoFuncionario() {
        initComponents();

    }

    public void inicializar(Funcionario f) {
        this.setVisible(true);
        fun = f;


        txtCodigo.setText(fun.codigo);
        txtNome.setText(fun.nome);
        txtDataNasc.setText(fun.getDataFormatada());
        txtNacionalidade.setText(fun.nacionalidade);
        txtNaturalidade.setText(fun.naturalidade);
        txtCargo.setText(fun.cargo);
        txtFuncao.setText(fun.funcao);
        txtDataAdmissao.setText(fun.getDataFormatadaAd());
        txtDataDemissao.setText(fun.getDataFormatadaDe());
        if (fun.getSexo() != null && fun.getSexo().equals("M")) {
            rdMasc.setSelected(true);
        } else if (fun.getSexo() != null && fun.getSexo().equals("F")) {
            rdFem.setSelected(true);
        }

        DefaultComboBoxModel model =  (DefaultComboBoxModel) cbEstadoCivil.getModel();

        for(int i=0; i<model.getSize(); i++){   
            String elemento = (String) model.getElementAt(i);
            if(elemento.equals(fun.getEstadocivil())){
               cbEstadoCivil.setSelectedIndex(i);
            }
        }        
        txtNomePai.setText(fun.nomepai);
        txtNomeMae.setText(fun.nomemae);

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Sexo = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtDataNasc = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtNaturalidade = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtNacionalidade = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtCargo = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtFuncao = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtDataAdmissao = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtDataDemissao = new javax.swing.JTextField();
        rdMasc = new javax.swing.JRadioButton();
        rdFem = new javax.swing.JRadioButton();
        cbEstadoCivil = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        txtNomePai = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtNomeMae = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        btnAvancar = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();
        banner = new javax.swing.JLabel();
        lblMensagem = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 706, 740));
        setMinimumSize(new java.awt.Dimension(720, 738));
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Arial", 1, 16));
        jLabel1.setForeground(new java.awt.Color(0, 102, 204));
        jLabel1.setText("Cadastro de Funcionário");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true), "Indentificação Pessoal", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 11))); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel3.setText("Nome : ");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(89, 60, -1, -1));
        jPanel1.add(txtNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 57, 511, -1));

        jLabel4.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel4.setText("Data de Nascimento :");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 86, -1, -1));
        jPanel1.add(txtDataNasc, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 83, 145, -1));

        jLabel5.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel5.setText("Sexo : ");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 86, -1, -1));

        jLabel8.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel8.setText("Estado Civil : ");
        jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(289, 34, -1, -1));
        jPanel1.add(txtNaturalidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 109, 245, -1));

        jLabel7.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel7.setText("Naturalidade : ");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(319, 112, -1, -1));
        jPanel1.add(txtNacionalidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 109, 145, -1));

        jLabel13.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel13.setText("Cargo : ");
        jPanel1.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(88, 138, -1, -1));
        jPanel1.add(txtCargo, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 135, 145, -1));

        jLabel14.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel14.setText("Função : ");
        jPanel1.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(348, 138, -1, -1));
        jPanel1.add(txtFuncao, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 135, 245, -1));

        jLabel6.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel6.setText("Nacionalidade : ");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(46, 112, -1, -1));

        jLabel2.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel2.setText("Codigo : ");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(82, 34, -1, -1));
        jPanel1.add(txtCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 31, 145, -1));

        jLabel9.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel9.setText("Data de Admissão : ");
        jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(22, 164, -1, -1));
        jPanel1.add(txtDataAdmissao, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 161, 145, -1));

        jLabel10.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel10.setText("Data de Demissão : ");
        jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(289, 164, -1, -1));
        jPanel1.add(txtDataDemissao, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 161, 245, -1));

        Sexo.add(rdMasc);
        rdMasc.setText("Masculino");
        jPanel1.add(rdMasc, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 82, -1, -1));

        Sexo.add(rdFem);
        rdFem.setText("Feminino");
        jPanel1.add(rdFem, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 80, -1, -1));

        cbEstadoCivil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " ", "Solteiro(a)", "Casado(a)", "Divorciado(a)", "Separado(a)", "Viuvo(a)", "Amasiado(a)" }));
        jPanel1.add(cbEstadoCivil, new org.netbeans.lib.awtextra.AbsoluteConstraints(365, 31, 280, 20));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true), "Filiação", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 11))); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtNomePai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomePaiActionPerformed(evt);
            }
        });
        jPanel2.add(txtNomePai, new org.netbeans.lib.awtextra.AbsoluteConstraints(138, 20, 508, -1));

        jLabel11.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel11.setText("Nome Mãe :");
        jPanel2.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(64, 49, -1, -1));
        jPanel2.add(txtNomeMae, new org.netbeans.lib.awtextra.AbsoluteConstraints(138, 46, 508, -1));

        jLabel12.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel12.setText("Nome Pai : ");
        jPanel2.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(68, 23, -1, -1));

        btnAvancar.setText("Avançar");
        btnAvancar.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true));
        btnAvancar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAvancarActionPerformed(evt);
            }
        });

        btnSair.setText("Sair");
        btnSair.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true));
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        banner.setIcon(new javax.swing.ImageIcon(getClass().getResource("/telasDeAlteracoes/banner.jpg"))); // NOI18N
        banner.setAutoscrolls(true);
        banner.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true));
        banner.setMaximumSize(new java.awt.Dimension(720, 187));
        banner.setMinimumSize(new java.awt.Dimension(720, 187));

        lblMensagem.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(248, 248, 248)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(lblMensagem, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 693, Short.MAX_VALUE))))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(553, Short.MAX_VALUE)
                .addComponent(btnAvancar, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
            .addComponent(banner, javax.swing.GroupLayout.PREFERRED_SIZE, 713, Short.MAX_VALUE)
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAvancar, btnSair});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(banner, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAvancar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(156, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnAvancar, btnSair});

        setBounds(150, 0, 719, 740);
    }// </editor-fold>//GEN-END:initComponents

    private void txtNomePaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomePaiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNomePaiActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void btnAvancarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAvancarActionPerformed

        Date dataNasc = null;
        boolean dataValida = false;

        try {
            String[] vData = txtDataNasc.getText().split("/");
            dataNasc = new Date(Integer.parseInt(vData[2]), Integer.parseInt(vData[1]) - 1, Integer.parseInt(vData[0]));
            dataValida = true;

        } catch (Exception e) {
            e.printStackTrace();
        }

        Date dataAdmi = null;
        boolean dataValidaAd = false;

        try {
            String[] vData = txtDataAdmissao.getText().split("/");
            dataAdmi = new Date(Integer.parseInt(vData[2]), Integer.parseInt(vData[1]) - 1, Integer.parseInt(vData[0]));
            dataValidaAd = true;

        } catch (Exception e) {
            e.printStackTrace();
        }

        Date dataDemi = null;
        boolean dataValidaDe = false;

       if(dataDemi != null){
        try {
            String[] vData = txtDataDemissao.getText().split("/");
            dataDemi = new Date(Integer.parseInt(vData[2]), Integer.parseInt(vData[1]) - 1, Integer.parseInt(vData[0]));
            dataValidaDe = true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        }


        if (txtCodigo.getText().trim().equals("")) {
            lblMensagem.setText("O campo codigo é obrigatório!");

        } else if (txtNome.getText().trim().equals("")) {
            lblMensagem.setText("O campo nome é obrigatório!");

        } else if (txtDataNasc.getText().trim().equals("")) {
            lblMensagem.setText("O campo data de nascimento é obrigatório!");

        } else if (!dataValida) {
            lblMensagem.setText("Data informada com formato inválido!");

        } else if (!dataValidaAd) {
            lblMensagem.setText("Data de Admissão informada com formato inválido!");        

        } else if (txtCargo.getText().trim().equals("")) {
            lblMensagem.setText("O campo cargo é obrigatório!");

        } else if (txtDataAdmissao.getText().trim().equals("")) {
            lblMensagem.setText("O campo data de admissão é obrigatório!");

        } else {
            //Bloco Identificação Pessoa
            fun.codigo = txtCodigo.getText().trim().toUpperCase();
            fun.nome = txtNome.getText().trim().toUpperCase();
            fun.setDtnasc(dataNasc);
            fun.nacionalidade = txtNacionalidade.getText().trim().toUpperCase();
            fun.naturalidade = txtNaturalidade.getText().trim().toUpperCase();
            fun.cargo = txtCargo.getText().trim().toUpperCase();
            fun.funcao = txtFuncao.getText().trim().toUpperCase();
            fun.setDataadmin(dataAdmi);
            fun.setDatademis(dataDemi);
            String estadoCivil = (String) cbEstadoCivil.getSelectedItem();
            fun.estadocivil = estadoCivil;

            //Bloco Filiação
            fun.nomepai = txtNomePai.getText().trim().toUpperCase();
            fun.nomemae = txtNomeMae.getText().trim().toUpperCase();

            if (rdMasc.isSelected()) {
                fun.sexo = "M";

            } else if (rdFem.isSelected()) {
                fun.sexo = "F";
            }

            AlteracaoFuncionario2 cadastro = new AlteracaoFuncionario2();
            cadastro.inicializar(fun);
            this.dispose();



        }

    }//GEN-LAST:event_btnAvancarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AlteracaoFuncionario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup Sexo;
    private javax.swing.JLabel banner;
    private javax.swing.JButton btnAvancar;
    private javax.swing.JButton btnSair;
    private javax.swing.JComboBox cbEstadoCivil;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblMensagem;
    private javax.swing.JRadioButton rdFem;
    private javax.swing.JRadioButton rdMasc;
    private javax.swing.JTextField txtCargo;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDataAdmissao;
    private javax.swing.JTextField txtDataDemissao;
    private javax.swing.JTextField txtDataNasc;
    private javax.swing.JTextField txtFuncao;
    private javax.swing.JTextField txtNacionalidade;
    private javax.swing.JTextField txtNaturalidade;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtNomeMae;
    private javax.swing.JTextField txtNomePai;
    // End of variables declaration//GEN-END:variables

    public void inicializar2(Funcionario fun) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
